package com.xq.tmall.controller.fore;

import com.xq.tmall.controller.BaseController;
import com.xq.tmall.entity.Product;
import com.xq.tmall.entity.User;
import com.xq.tmall.service.LookRecordService;
import com.xq.tmall.service.UserService;
import com.xq.tmall.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户浏览记录
 *
 * @ClassName LookReCrod
 * @Description TODO
 * @date 2022/1/25 15:46
 * @Version 1.0
 */
@Controller
public class LookRecordController extends BaseController {
    @Autowired
    private LookRecordService lookRecordService;
    @Autowired
    private UserService userService;


    /**
     * 添加用户浏览记录信息
     */
    @RequestMapping(value = "/browse", method = RequestMethod.GET)
    public String getLookRecrod(HttpSession session, Map<String,Object> map){
        logger.info("检查用户是否登录");
        Object userId = checkUser(session);
        if (userId != null) {
            logger.info("获取用户信息");
            User user = userService.get(Integer.parseInt(userId.toString()));
            map.put("user", user);
        }else {
            logger.info("用户未登录-跳转登录界面");
            return "redirect:/login";
        }

        logger.info("整合搜索信息");
        Product product = new Product();
        String searchValue = null;
        Integer searchType = null;


        //产品总数量
        Integer productCount;
        //分页工具
        PageUtil pageUtil = new PageUtil(0, 20);
        //浏览记录商品信息详情列表
        List<Product> productList= lookRecordService.getLookRecrod(Integer.parseInt(userId.toString()), pageUtil);


        map.put("totalPage", productList.size());
        map.put("pageUtil", pageUtil);
        map.put("productList", productList);
        map.put("searchValue", searchValue);
        map.put("searchType", searchType);

        return "fore/browse";
    }
}
